<?php

namespace app\admin\model;
use think\Model;

/**
 * This is the model class for table "hxdc_xcx_customer".
 *
 * @property int $id
 * @property int $member_id 用户id
 * @property int $project_id 项目id
 * @property int $agent_id 经济人id
 * @property int $kf_id 顾问id
 * @property string $nickname 被推荐客户姓名
 * @property string $mobile 被推荐客户手机号
 * @property int $progress_int -1无效;0审核中;1已报备;4签约;5申请结佣;6已结佣
 * @property int $status -1无效;0审核中;1有效;
 * @property int $a1_time 报备/无效时间
 * @property int $a2_time 到访时间
 * @property int $a3_time 认购时间
 * @property int $a4_time 签约时间
 * @property int $a5_time 申请结佣时间
 * @property int $a6_time 发放时间，结佣时间
 * @property int $commission 佣金
 * @property int $is_noticed 0未通知过；1已通知过
 * @property int $created_at 推荐时间
 */
class XcxCustomer extends Model
{
    public static function paramsNew() :array {
        return array(
            'member_id' => 0,
            'project_id' => 0,
            'agent_id' => 0,
            'kf_id' => 0,
            'nickname' => '',
            'mobile' => '',
            'progress_int' => 0,
            'a1_info' => '',
            'a2_info' => '',
            'a3_info' => '',
            'a4_info' => '',
            'a1_time' => 0,
            'a2_time' => 0,
            'a3_time' => 0,
            'a4_time' =>0,
            'a5_time' => 0,
            'a6_time' => 0,
            'commission' => 0,
            'is_noticed' => 0,
            'merchant_id' => 0,
            'status' => 1,
            'created_at' => time(),
            'customerid' => 0,
        );
    }
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_xcx_customer';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id', 'project_id', 'agent_id', 'kf_id', 'progress_int', 'status', 'a1_time', 'a2_time', 'a3_time', 'a4_time', 'a5_time', 'a6_time', 'commission', 'is_noticed', 'created_at'], 'integer'],
            [['agent_id', 'kf_id', 'project_id', 'nickname', 'mobile'], 'required'],
            [['nickname', 'mobile'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'merchant_id' => '用户 ID',
            'member_id' => '用户 ID',
            'project_id' => '项目 ID',
            'agent_id' => '经济人 ID',
            'kf_id' => '顾问 ID',
            'nickname' => '被推荐客户姓名',
            'mobile' => '被推荐客户手机号',
            'progress_int' => '进度',
            'status' => '状态',
            'a1_time' => '报备/无效时间',
            'a2_time' => '到访时间',
            'a3_time' => '认购时间',
            'a4_time' => '签约时间',
            'a5_time' => '申请结佣时间',
            'a6_time' => '结佣时间',
            'commission' => '佣金',
            'is_noticed' => '是否已通知过',
            'created_at' => '推荐时间',
        ];
    }


    /**
     * 推荐顾问
     */
    public function getKf()
    {
        return $this->hasOne(XcxKf::class, ['id' => 'kf_id']);
    }

    /**
     * 推荐项目
     */
    public function getProject()
    {
        return $this->hasOne(Project::class, ['id' => 'project_id']);
    }
    /**
     * 推荐经纪人
     */
    public function getAgent()
    {
        return $this->hasOne(XcxAgent::class, ['id' => 'agent_id']);
    }

    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}

